﻿/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
*/

package com.chasekernan.hxnova.componenteditor;

import com.chasekernan.hxnova.core.components.Armor;
import com.chasekernan.hxnova.core.components.ComponentSet;
import com.chasekernan.hxnova.core.components.Shield;
import flash.display.Loader;
import flash.errors.IOError;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.system.Security;
import org.aswing.AsWingManager;
//import org.aswing.skinbuilder.orange.OrangeLookAndFeel;
import flash.Lib;
import org.aswing.UIManager;
import org.aswing.AsWingUtils;
import org.aswing.plaf.basic.BasicLookAndFeel;
import flash.events.Event;
import com.chasekernan.hxnova.componenteditor.HullEditor;
import com.chasekernan.hxnova.core.components.ComponentSet;
import com.chasekernan.hxnova.componenteditor.HullPartEditor;
import org.aswing.JFrame;
import org.aswing.geom.IntDimension;
import org.aswing.JPanel;
import org.aswing.JLabel;
import flash.display.StageScaleMode;
import org.aswing.geom.IntRectangle;

class ComponentEditorMain {
    
    
    public static function main() {
        Security.allowInsecureDomain("localhost", "www.chasekernan.com", "C:\\");
		Lib.current.stage.scaleMode = StageScaleMode.NO_SCALE;
        
        AsWingManager.setRoot(Lib.current);
        
        #if loadLookAndFeel
		trace("load");
        //load look and feel
        var loader = new Loader();
        var loaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
        
		//TODO: Won't work in haxe 2.0 because OrangeLookAndFeel is trying to access a protected function in its super class.
        var finishedLoading = function(?e : Dynamic) {
			try {
				trace("finished");
				var laf = cast (ApplicationDomain.currentDomain.getDefinition("org.aswing.skinbuilder.orange.OrangeLookAndFeel"), Class<Dynamic>);
				
				UIManager.setLookAndFeel(Type.createInstance(laf, []));
				AsWingUtils.updateAllComponentUIInMemory();
			} catch (e : Dynamic) {
				trace("E: " + e);
			}
        }
		
		var traceSomething = function(?e : Dynamic) {
			trace("Something: " + e);
		};
		
		loader.load(new URLRequest("OrangeLAF.swf"));//, loaderContext);
        
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, finishedLoading);
		
		loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, traceSomething);
		
		loader.contentLoaderInfo.addEventListener(Event.OPEN, traceSomething);
		loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, traceSomething);
        #end
		
		var window = new ComponentEditorWindow(Lib.current);
        window.show();
    }
	
	public function new() {
		main();
	}
    
}